<template>
  <div class="main">

    <img class="pic" :alt="'ColinWoo ' + article.article_title" :src="article.image.file_path" />
    <h1 :data-title="article.article_title">{{article.article_title}}</h1>
    <p class="time"><span>{{article.create_time.substring(0,2)}}</span>{{article.create_time.substring(2)}} Edited  by ColinWoo</p>
    <div class="syllabus" v-show="syllabus_show">
      <div class="syllabus-title">大纲</div>
      <ul>
        <li v-html="items.html" v-for="(items,index) in syllabus" :key="index" @click="toHash(items.title)">
        </li>
      </ul>
    </div>
    <div id="content" class="content markdown-body" v-html="html"></div>
  </div>
</template>

<script>
import Hljs from 'highlight.js';
import 'github-markdown-css/github-markdown.css'
import 'highlight.js/styles/xcode.css'
import syllabus from 'match-syllabus/index.js'
export default {
  // head(){
  //   return {
  //     script:[
  //       {src:'/js/matchSyllabus.js'}
  //     ]
  //   }
  // },
  data(){
     return {
       syllabus_show:false
     }
  },
  created(){

  },
  mounted () {
    window.addEventListener('scroll', this.handleScroll)
  },
  methods: {
    toHash(path){
      document.querySelector('#'+path).scrollIntoView(true)
    },
    handleScroll(el) {
      let scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
      if(scrollTop > 650 && this.syllabus.length > 0)
      {
        this.syllabus_show = true;
      }else
      {
        this.syllabus_show = false;
      }
    },
  },
  async asyncData(that) {
    if(that.store.state.menus.menusList.length < 1)
    {
      await that.store.dispatch("menus/menusList",{});
    }

    let articleInfo = await that.store.dispatch("article/articleInfo", {article_id:that.route.params.article_id}).then(res=>{
      return res.data
    })

    let hljs = require('highlight.js'); // https://highlightjs.org/

    // 通常的默认值们
    let md = require('markdown-it')({
      highlight: function (str, lang) {
        if (lang && hljs.getLanguage(lang)) {
          try {
            return hljs.highlight(lang, str).value;
          } catch (__) {}
        }

        return ''; // 使用额外的默认转义
      }
    });
    //官方示例
    // let converter = new showdown.Converter();
    // text可以理解为 我们输入的文章内容
    // HTML是我们编辑后MarkDown转换成的浏览器可以解析的页面
    // this.html = converter.makeHtml(this.article.article_content);


    // 将MarkDown文档转成HTML
    let html = md.render(articleInfo.article_content);
    let result = syllabus(html)

    return {
      article:articleInfo,
      html:result.content,
      syllabus:result.syllabus
    }
  }
}
</script>

<style scoped>
@media screen and (max-width: 1000px) {
  .syllabus{
    display: none;
  }
  .main{
    height: 100vh;
    display: block;
    width: 100%;
    padding-top:30px;
    margin: 0 auto;
  }
  .markdown-body {
    box-sizing: border-box;
    width: 90%;
    margin: 0 auto;
    padding-bottom: 45px;
  }
  .main >h1 {
    margin-bottom: 20px;
    text-align: center;
    font-size: 24px;
    font-weight: normal;
    color: #404040;
  }
  .main >p{
    margin-bottom: 10px;
    text-align: center;
  }
  .main > .content{
    margin-top: 0px;
  }
  .pic{
    display: block;
    width: 100%;
    height: 36%;
    margin-bottom: 40px;
  }
  .time{
    color: #404040;
    font-size: 18px;
    font-weight: 800;
    display: block;
  }
  .time > span {
    color: #fb5353;
    vertical-align:top;
  }
}
@media screen and (min-width: 1000px) {
  .syllabus{
    position:fixed;
    left:0;
    top: 50%;
    display: block;
    height: 98%;
    width: 24%;
    overflow: hidden;
    top:0;
    background-color: #fff;
    border-right:solid 1px #f2f2f2;
    padding: 20px;
  }
  .syllabus-title{
    font-size: 48px;
    text-align: center;
  }
  .syllabus:hover{
    overflow-y:scroll;
  }
  .syllabus > ul > li{
    margin:8px;
    line-height: 18px;
  }
  .syllabus > ul > li >>> h1 {
    font-size:16px;
    font-weight: 600;
  }
  .syllabus > ul > li >>> h2 {
    font-size:14px;
    font-weight: 600;
    text-indent:2em
  }
  .syllabus > ul > li >>> h3 {
    font-size:12px;
    font-weight: 400;
    text-indent:3em
  }
  .syllabus > ul > li >>> h4 {
    font-size:12px;
    font-weight: 400;
    text-indent:4em
  }
  .syllabus > ul > li >>> h5 {
    font-size:12px;
    font-weight: 400;
    text-indent:5em
  }
  .main{
    height: 100vh;
    display: inline-block;
    width: 70%;
    /* border:solid 1px red; */
    padding-top:30px;
    vertical-align:top;
    margin-left: 50px;
  }
  .markdown-body {
    box-sizing: border-box;
    width: 90%;
    margin: 0 auto;
    padding: 45px;
  }
  .main >h1 {
    margin-bottom: 20px;
    text-align: center;
    font-size: 36px;
    font-weight: normal;
    color: #404040;
  }
  .main >p{
    margin-bottom: 10px;
    text-align: center;
  }
  .main > .content{
    margin-top: 0px;
  }
  .pic{
    display: block;
    width: 100%;
    height: 65%;
    margin-bottom: 40px;
  }
  .time{
    color: #404040;
    font-size: 24px;
    font-weight: 800;
    display: block;
  }
  .time > span {
    color: #fb5353;
    vertical-align:top;
  }
}


</style>
